package com.situ.web.service.impl;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.internal.compiler.lookup.ImplicitNullAnnotationVerifier;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.impl.StudentDaoImpl;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private IStudentDao studentDao = new StudentDaoImpl();

	@Override
	public List<Student> selectAll() {
		// 体现Service业务逻辑，给所有学生名字前面加上"Java1907"
		List<Student> list = studentDao.selectAll();
		for (Student student : list) {
			String name = student.getName();
			student.setName("Java1907-" + name);
		}
		
		return list;
	}

	@Override
	public List<Student> search(String name) {
		return studentDao.search(name);
	}

	@Override
	public boolean insert(Student student) {
		// 1、数据库中查看是不是有该姓名的学生 selectByName(name)
		// 2.1、如果有：表示该名字用户已经存在,不去调用insert
		// 2.2、如果没有：表示该名字用户不存在,去调用insert插入这个用户
		// select count(*) from student where name='zhansgan';
		List<Student> list =  studentDao.selectByName(student.getName());
		if (list.size() > 0) {
			System.out.println("该用户已经存在");
			// 该名字用户已经存在
			return false;
		} else {
			int count = studentDao.insert(student);
			return count == 1 ? true : false;
		}
	}

	@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
				
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		// 业务逻辑：id在10-100之间的不删除
		int count = studentDao.deleteById(id);
		
		return count == 1 ? true : false;
	}

	@Override
	public PageBean pageList(Integer pageNo, Integer pageSize) {
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(pageNo);
		// 一页有多少条数据
		pageBean.setPageSize(pageSize);
		// 一共有多少页
		int count = studentDao.getTotalCount();
		int totalPage = (int) Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);
		// 当前页数据
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentDao.pageList(offset, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	public Student selectById(int id) {
		return studentDao.selectById(id);
	}

	
	public static void main(String[] args) {
		int count = 10;
		int pageSize = 3;
		// Type mismatch: cannot convert from double to int
		int totalPage = (int) Math.ceil((double)count / pageSize);
		System.out.println(totalPage);
	}

	@Override
	public boolean checkName(String name) {
		List<Student> list = studentDao.selectByName(name);
//		if (list.size() > 0) {
//			return true;
//		} else {
//			return false;
//		}
		return list.size() > 0 ? true : false;
	}

	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		return studentDao.selectStudentBanjiInfo();
	}
}
